//
//  CCAlertViewController.m
//  Activity
//
//  Created by its on 15/6/5.
//  Copyright (c) 2015年 coco. All rights reserved.
//

#import "CCAlertViewController.h"
#import "MJRefresh.h"
#import <AVOSCloud/AVOSCloud.h>
#import <Masonry/Masonry.h>
#import "CCAlertViewModel.h"

@interface CCAlertViewController ()
@property (nonatomic, strong) UIScrollView *naviScrollView;
@property (nonatomic, strong) UIView *naviView;
@property (nonatomic, strong) UIScrollView *contentScrollView;
@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, strong) CCAlertViewModel *viewModel;
@end

@implementation CCAlertViewController

- (id) initWithViewModel:(CCAlertViewModel *) viewModel {
  if (self = [super init]) {
    _viewModel = viewModel;
  }
  return self;
}

- (void)viewDidLoad {
  self.title = @"提醒";
  [self setupUI];
  [self setupCSS];
  [self initNaviView];
  [self initContentView];
  [super viewDidLoad];
}

- (void) viewDidAppear:(BOOL)animated {
  [self tableViewDidShow:1];
  [super viewDidAppear:animated];
}

- (void) setupUI {
  self.naviScrollView = [[UIScrollView alloc] init];
  self.naviView = [UIView new];
  [self.naviScrollView addSubview: self.naviView];
  self.naviScrollView.showsHorizontalScrollIndicator = NO;
  self.contentScrollView = [[UIScrollView alloc] init];
  self.contentScrollView.pagingEnabled = YES;
  self.contentScrollView.delegate = self;
  self.contentView = [UIView new];
  self.contentView.backgroundColor = [UIColor blueColor];
  [self.contentScrollView addSubview: self.contentView];
  [self.view addSubview: self.naviScrollView];
  [self.view addSubview:self.contentScrollView];
}

- (void) setupCSS {
  [self.naviScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.view);
    make.right.equalTo(self.view);
    make.top.equalTo(self.view);
    make.height.equalTo(@(40));
  }];
  
  [self.naviView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.equalTo(self.naviScrollView);
    make.height.equalTo(self.naviScrollView);
  }];
  
  [self.contentScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.view);
    make.right.equalTo(self.view);
    make.top.equalTo(self.naviScrollView.mas_bottom);
    make.bottom.equalTo(self.view);
  }];
  
  [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.equalTo(self.contentScrollView);
    make.height.equalTo(self.contentScrollView);
  }];
}

- (void) initNaviView {
  NSArray *btnTitle = @[@"评论", @"回复", @"消息"];
  UIView *lastView = nil;
  for (int i = 0; i < btnTitle.count; i++) {
    UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    titleButton.tag = i + 1;
    if (i == 0 ) {
      titleButton.selected = YES;
    }
    [titleButton setTitle:btnTitle[i] forState:UIControlStateNormal];
    [titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    NSAttributedString *titleStr = [[NSAttributedString alloc] initWithString:btnTitle[i]
                                                                   attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}];
    NSAttributedString *titleStr2 = [[NSAttributedString alloc] initWithString:btnTitle[i]
                                                                   attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15],
                                                                                NSForegroundColorAttributeName:RGB(46, 169, 56, 1)
                                                                                }];

    [titleButton setAttributedTitle:titleStr forState:UIControlStateNormal];
    [titleButton setAttributedTitle:titleStr2 forState:UIControlStateSelected];
    @weakify(self);
    titleButton.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(UIButton *input) {
      @strongify(self);
      [self.contentScrollView scrollRectToVisible:CGRectMake(CGRectGetWidth(self.contentScrollView.frame)*(input.tag-1),
                                                            CGRectGetMinY(self.contentScrollView.frame),
                                                            CGRectGetWidth(self.contentScrollView.frame),
                                                             CGRectGetHeight(self.contentScrollView.frame)) animated:YES];
      UIButton *button = (UIButton *)[self.naviView viewWithTag:self.viewModel.selectedButtonTag];
      button.selected = NO;
      self.viewModel.selectedButtonTag = input.tag;
      input.selected = YES;
      return [RACSignal empty];
    }];
    [self.naviView addSubview: titleButton];
    [titleButton mas_makeConstraints:^(MASConstraintMaker *make) {
      make.left.equalTo(lastView?lastView.mas_right:@(0));
      make.top.equalTo(self.naviView);
      make.bottom.equalTo(self.naviView);
      make.width.equalTo(@(DEVICEWIDTH/3));
    }];
    lastView = titleButton;
  }
  [self.naviView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.right.equalTo(lastView);
  }];
}

- (void) initContentView {

  UIView *lastView = nil;
  for (int i = 0; i < 3; i ++) {
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero
                                                          style:UITableViewStylePlain];
    tableView.tag = i + 1;
    tableView.delegate = self;
    tableView.dataSource = self;
    [tableView addHeaderWithCallback:^{
    }];
    [self.contentView addSubview: tableView];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
      make.left.equalTo(lastView?lastView.mas_right:@(0));
      make.top.equalTo(self.contentView);
      make.bottom.equalTo(self.contentView);
      make.width.equalTo(@(DEVICEWIDTH));
    }];
    
    lastView = tableView;
  }
  
  [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.right.equalTo(lastView);
  }];
}

- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
}

#pragma mark private
- (void) tableViewDidShow:(int) index {
  UITableView *tableView = (UITableView *)[self.contentView viewWithTag:index];
  [tableView headerBeginRefreshing];
  UIButton *button = (UIButton *)[self.naviView viewWithTag:self.viewModel.selectedButtonTag];
  button.selected = NO;
  UIButton *selectedButton = (UIButton *)[self.naviView viewWithTag:index];
  self.viewModel.selectedButtonTag = index;
  selectedButton.selected = YES;
}
#pragma mark tableview datasource

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"hhehe"];
  if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"hhehe"];
  }
  cell.textLabel.text = @"有事没事真是事";
  return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return 100;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  return 80;
}

#pragma mark scrollView delegate 

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  if (scrollView == self.contentScrollView) {
    int tag = (scrollView.contentOffset.x/scrollView.bounds.size.width) + 1;
    [self tableViewDidShow:tag];
  }
}

@end
